package com.td.module.produce.service.inputhusbandry;

import com.td.framework.common.pojo.PageResult;
import com.td.module.produce.controller.admin.inputhusbandry.vo.InputHusbandryCreateReqVO;
import com.td.module.produce.controller.admin.inputhusbandry.vo.InputHusbandryExportReqVO;
import com.td.module.produce.controller.admin.inputhusbandry.vo.InputHusbandryPageReqVO;
import com.td.module.produce.controller.admin.inputhusbandry.vo.InputHusbandryUpdateReqVO;
import com.td.module.produce.convert.inputhusbandry.InputHusbandryConvert;
import com.td.module.produce.dal.dataobject.inputhusbandry.InputHusbandryDO;
import com.td.module.produce.dal.mysql.inputhusbandry.InputHusbandryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.produce.enums.ErrorCodeConstants.INPUT_HUSBANDRY_NOT_EXISTS;

/**
 * 投入品与农事中间 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class InputHusbandryServiceImpl implements InputHusbandryService {

    @Resource
    private InputHusbandryMapper inputHusbandryMapper;

    @Override
    public Long createInputHusbandry(InputHusbandryCreateReqVO createReqVO) {
        // 插入
        InputHusbandryDO inputHusbandry = InputHusbandryConvert.INSTANCE.convert(createReqVO);
        inputHusbandryMapper.insert(inputHusbandry);
        // 返回
        return inputHusbandry.getId();
    }

    @Override
    public void updateInputHusbandry(InputHusbandryUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateInputHusbandryExists(updateReqVO.getId());
        // 更新
        InputHusbandryDO updateObj = InputHusbandryConvert.INSTANCE.convert(updateReqVO);
        inputHusbandryMapper.updateById(updateObj);
    }

    @Override
    public void deleteInputHusbandry(Long id) {
        // 校验存在
        this.validateInputHusbandryExists(id);
        // 删除
        inputHusbandryMapper.deleteById(id);
    }

    private void validateInputHusbandryExists(Long id) {
        if (inputHusbandryMapper.selectById(id) == null) {
            throw exception(INPUT_HUSBANDRY_NOT_EXISTS);
        }
    }

    @Override
    public InputHusbandryDO getInputHusbandry(Long id) {
        return inputHusbandryMapper.selectById(id);
    }

    @Override
    public List<InputHusbandryDO> getInputHusbandryList(Collection<Long> ids) {
        return inputHusbandryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InputHusbandryDO> getInputHusbandryPage(InputHusbandryPageReqVO pageReqVO) {
        return inputHusbandryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InputHusbandryDO> getInputHusbandryList(InputHusbandryExportReqVO exportReqVO) {
        return inputHusbandryMapper.selectList(exportReqVO);
    }

}
